from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
from .neo4j_connection import graph


class ActionAskAboutAlias(Action):
    def name(self) -> Text:
        return "action_ask_about_alias"

    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        person_name = next(tracker.get_latest_entity_values("person"), None)
        person_list = graph.query_person(person_name)

        if not person_list:
            dispatcher.utter_message("不好意思，我不清楚你问的" + (person_name if person_name else "") + "是谁。", attachment="finish")
            return []

        alias = graph.query_person_alias(person_name)

        if not alias:
            dispatcher.utter_message("你直接称呼" + person_name + "的名字就可以了，我想他应该不会介意的。", attachment="finish")
            return []

        message = ("你可以用" + alias[0]["alias"] + person_name + "称呼" + person_name + "当然你直接称呼他的名字也可以，我想他应该不会介意的。")

        dispatcher.utter_message(message, attachment="finish")
        return []
